use regex::Regex;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::helper::extract_combined_index;

/// 协议状态信息，目前华为有该数据
#[derive(Debug, Serialize, Deserialize, Default, JsonSchema, Clone)]
pub struct ProtocolStatistic {
    /// 协议名称
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.2.0 = STRING: "DNS"
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.2.1 = STRING: "HTTP"
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.2.2 = STRING: "HTTPS"
    pub name: String,

    /// 在当前时间前的接收报文数
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.4.0 = Counter64: 1
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.4.1 = Counter64: 2
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.4.2 = Counter64: 3
    pub receive_count: String,

    /// 在当前时间前的丢弃报文数
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.5.0 = Counter64: 4
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.5.1 = Counter64: 5
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.5.2 = Counter64: 6
    pub drop_count: String,

    /// 丢包率
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.6.0 = STRING: "0%"
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.6.1 = STRING: "1%"
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.6.2 = STRING: "2%"
    pub drop_percent: String,

    /// 会话的连接请求数
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.7.0 = Counter64: 7
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.7.1 = Counter64: 8
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.7.2 = Counter64: 9
    pub request_count: String,

    /// 成功建立会话的数量
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.8.0 = Counter64: 100
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.8.1 = Counter64: 110
    ///
    /// iso.3.6.1.4.1.2011.6.122.15.5.3.1.8.2 = Counter64: 120
    pub success_establish: String,
}

impl ProtocolStatistic {
    pub fn values(value: &str) -> Vec<Self> {
        let re = Regex::new(r".3.6.1.4.1.2011.6.122.15.5.3.1.2.(\d+)").unwrap();
        let mut results = vec![];
        for cap in re.captures_iter(value) {
            let index = &cap[1];
            results.push(ProtocolStatistic {
                name: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.2.",
                    index,
                    r#" = STRING: "(.+)""#,
                    value,
                ),
                receive_count: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.4.",
                    index,
                    r#" = Counter64: (\d+)"#,
                    value,
                ),
                drop_count: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.5.",
                    index,
                    r#" = Counter64: (\d+)"#,
                    value,
                ),
                drop_percent: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.6.",
                    index,
                    r#" = STRING: "(.+)""#,
                    value,
                ),
                request_count: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.7.",
                    index,
                    r#" = Counter64: (\d+)"#,
                    value,
                ),
                success_establish: extract_combined_index(
                    ".3.6.1.4.1.2011.6.122.15.5.3.1.8.",
                    index,
                    r#" = Counter64: (\d+)"#,
                    value,
                ),
            })
        }
        results
    }
}
